package com.winskysoft.velocity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import java.sql.SQLException;
import java.util.*;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.io.VelocityWriter;

import org.apache.velocity.Template;

import org.apache.velocity.VelocityContext;

import com.winskysoft.database.DatabaseManager;
import com.winskysoft.entity.Column;
import com.winskysoft.entity.Table;
import com.winskysoft.tool.DatabaseTool;

/**
 * 
 * VelocityTool
 * 
 * @author battler
 * @date 2013-1-26 下午2:29:52
 * @ver 0.1
 */
public class VelocityTool {
	public static void writeTemp(String projectName, String velocityUrl,
			String format, Table table, String url, String type)
			throws IOException {

		if (table.getName().toUpperCase().startsWith("SYS_")) {
			return;
		}

		VelocityEngine ve = new VelocityEngine();
		ve.setProperty("resource.loader", "class");
		ve.setProperty("class.resource.loader.class",
				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
		ve.init();

		// 取得velocity的模版

		Template t = ve.getTemplate(velocityUrl, "UTF-8");

		// 取得velocity的上下文context

		VelocityContext context = new VelocityContext();

		// 新建

		// for (Table table : tables) {
		// 把数据填入上下文

		context.put("table", table);
		context.put("projectName", projectName);

		// 输出流

		StringWriter writer = new StringWriter();

		String path = url;
		String[] strs = table.getName().split("_");

		String nameStr = table.getName() + type;
		File file = new File(path + "/" + nameStr + ".java");
		File fileDir = new File(path + "/");
		boolean b = fileDir.mkdirs();
		FileWriter fileWriter = new FileWriter(file);

		// 转换输出
		t.merge(context, fileWriter);

		fileWriter.flush();
		fileWriter.close();
		// }

	}

	public static void main(String[] args) throws Exception {

		String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
		String username = "root";
		String password = "root";
		try {
			DatabaseManager databaseManager = DatabaseManager
					.getDatabaseManager(url, username, password);
			List<String[]> set = databaseManager.queryList("show tables;");
			for (String[] s : set) {
				Table table = new Table();
				table.setTable(s[0]);
				List<Column> columnList = new ArrayList<Column>();
				table.setColumn(columnList);
				List<String[]> list = databaseManager.queryList("describe "
						+ s[0]);
				for (String[] x : list) {
					Column column = new Column();
					column.setColumn(x[0]);
					column.setType(x[1]);
					columnList.add(column);
					// for(String str:x){
					// System.out.print(str+"   ");
					// }
					// System.out.println();
				}
				// System.out.println();

				// 初始化并取得Velocity引擎

				VelocityEngine ve = new VelocityEngine();

				ve.init();

				// 取得velocity的模版

				Template t = ve.getTemplate("/template/mentity.vm", "UTF-8");

				// 取得velocity的上下文context

				VelocityContext context = new VelocityContext();

				// 把数据填入上下文

				context.put("table", table);

				// 输出流

				StringWriter writer = new StringWriter();

				String path = VelocityTool.class.getResource("/").getPath();

				File file = new File(path + "/" + table.getTable() + ".java");
				FileWriter fileWriter = new FileWriter(file);

				// 转换输出
				t.merge(context, fileWriter);

				fileWriter.flush();
				fileWriter.close();

				// System.out.println(writer.toString());

				return;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
}